import {setUploadImage,reDeleteImage} from "../../../api/moretap"
import {useStore} from "vuex";
import {onMounted,reactive,ref} from "vue"
export function usesetUploadImage(){
	const {state} = useStore()
	const BASE_URL = import.meta.env.VITE_BASE_API
   function initsetUploadImage(params:{
	   url:string
	   CLASS_ID:string
	   STATE:string
	   WX_PIC:string
	   isload:boolean
	   wx_pic:string
   }){
	 return new Promise((resolve,reject)=>{
		 if(params.isload){
			 setUploadImage({
			 	CLASS_ID:params?.CLASS_ID,
			 	STATE :params?.STATE,
			 	WX_PIC:params?.wx_pic,
			 	OPERATOR_ID:state.user.userInfo.CUSER_ID
			 })
			 .then(res=>{
			 	if (res.ERROR[0].errcode == '0'){
			 		uni.showToast({
			 			title: "修改成功",
			 			icon: 'none',
			 			mask: true
			 		})
					setTimeout(()=>{
						uni.navigateBack({
							delta:1
						})
					},500)
			 		resolve(res)
			 	}else {
			 		uni.showToast({
			 			title: res.ERROR[0].error,
			 			icon: 'none',
			 			mask: true
			 		})
			 		reject('err')
			 	}
			 })
			 .catch(err=>{
			 	uni.showToast({
			 		title: '修改失败',
			 		icon: 'error',
			 		mask: true
			 	})
			 	reject('err')
			 })
		 }else{
			 uni.uploadFile({
			 	url: BASE_URL + '/pic_server/upload', //服务器 url
			 	filePath: params?.url, // 要上传文件资源的路径
			 	name: 'file', // 文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容
			 	formData:{
			 		token:uni.getStorageSync("token"),
			 		CUSER_ID:state.user.userInfo.CUSER_ID,
			 		FLAG:"WQ",
			 	},
			 	success(res) {
			 		let tmpData = JSON.parse(res.data)
			 				setUploadImage({
			 					CLASS_ID:params?.CLASS_ID,
			 					STATE :params?.STATE,
			 					WX_PIC:tmpData.DATA.filename,
			 					OPERATOR_ID:state.user.userInfo.CUSER_ID
			 				})
			 				.then(res=>{
			 					if (res.ERROR[0].errcode == '0'){
									if(params.wx_pic){
										reDeleteImage({
											picfilename:params.wx_pic
										})
										.then(res=>{
											uni.showToast({
												title: "修改成功",
												icon: 'none',
												mask: true
											})
											setTimeout(()=>{
												uni.navigateBack({
													delta:1
												})
											},500)
										})
									}else{
										uni.showToast({
											title: "修改成功",
											icon: 'none',
											mask: true
										})
										setTimeout(()=>{
											uni.navigateBack({
												delta:1
											})
										},500)
									}
			 						resolve(res)
			 					}else {
			 						uni.showToast({
			 							title: res.ERROR[0].error,
			 							icon: 'none',
			 							mask: true
			 						})
			 						reject('err')
			 					}
			 				})
			 				.catch(err=>{
								reDeleteImage({
									picfilename:tmpData.DATA.filename
								})
								.then(res=>{
									//修改失败删除当前上传照片
								})
			 					uni.showToast({
			 						title: '修改失败',
			 						icon: 'error',
			 						mask: true
			 					})
			 					reject('err')
			 				})
			 	},
			 	fail(err) {
			 	},
			 	complete() {
			 	},
			 })
		 }
	 })  
   }
   return{
	   initsetUploadImage
   }
}